package com.ln.lnzw.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.AddCollectBean;
import com.ln.lnzw.bean.IsCollectBean;
import com.ln.lnzw.bean.RegistBean;
import com.ln.lnzw.bean.WorkDetailsBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.WorkDetailsUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/23.
 * 办事服务表单页
 */

public class HandleaffairsServiceFormsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_shoucang)
    ImageView ivShoucang;
    @BindView(R.id.textview1)
    TextView textview1;
    @BindView(R.id.textview2)
    TextView textview2;
    @BindView(R.id.ll_zixun)
    LinearLayout llZixun;
    @BindView(R.id.ll_shenqing)
    LinearLayout llShenqing;
    @BindView(R.id.tv_content1)
    TextView tvContent1;
    @BindView(R.id.tv_content2)
    TextView tvContent2;
    @BindView(R.id.tv_content3)
    TextView tvContent3;
    @BindView(R.id.tv_content4)
    TextView tvContent4;
    @BindView(R.id.tv_content5)
    TextView tvContent5;
    @BindView(R.id.tv_content6)
    TextView tvContent6;
    @BindView(R.id.tv_content7)
    TextView tvContent7;
    @BindView(R.id.tv_content8)
    TextView tvContent8;
    @BindView(R.id.tv_content9)
    TextView tvContent9;
    @BindView(R.id.tv_content10)
    TextView tvContent10;
    @BindView(R.id.tv_content11)
    TextView tvContent11;
    @BindView(R.id.tv_content12)
    TextView tvContent12;
    @BindView(R.id.tv_content13)
    TextView tvContent13;
    @BindView(R.id.tv_content14)
    TextView tvContent14;
    @BindView(R.id.tv_content15)
    TextView tvContent15;
    @BindView(R.id.tv_content16)
    TextView tvContent16;
    @BindView(R.id.rl_shenqingtiaojian)
    RelativeLayout rlShenqingtiaojian;
    @BindView(R.id.rl_shenqingcailiao)
    RelativeLayout rlShenqingcailiao;
    @BindView(R.id.rl_banliliucheng)
    RelativeLayout rlBanliliucheng;
    @BindView(R.id.rl_shoufeiqingkuang)
    RelativeLayout rlShoufeiqingkuang;
    @BindView(R.id.rl_zhongjiefuwu)
    RelativeLayout rlZhongjiefuwu;
    @BindView(R.id.rl_shenpijieguo)
    RelativeLayout rlShenpijieguo;
    @BindView(R.id.rl_jiujitujing)
    RelativeLayout rlJiujitujing;
    @BindView(R.id.rl_changjianwenti)
    RelativeLayout rlChangjianwenti;
    @BindView(R.id.rl_banjiegongshi)
    RelativeLayout rlBanjiegongshi;
    private String itemId;
    private WorkDetailsBean.ResultBean.DeptBean deptBean;
    private WorkDetailsBean.ResultBean.DeptExpandBean expandBean;
    private Bundle b;
    private String  itemTitle;
    private String  itemCode;
   private Boolean isCollect=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handleaffairs_service_forms);
        ButterKnife.bind(this);
        itemId = getIntent().getStringExtra("itemId");
        itemTitle = getIntent().getStringExtra("itemTitle");
        itemCode = getIntent().getStringExtra("itemCode");
        Log.i("123", "onCreate: " + itemId);
        isCollect();
        initData();
    }
    //是否收藏
    private void isCollect() {
        Log.i("000", "USER_UID: "+spUtils.getString(AppConstant.USER_UID));
        Log.i("000", "TOKEN: "+spUtils.getString(AppConstant.TOKEN));
        Log.i("000", "itemCode: "+itemCode);
        Log.i("000", "itemTitle: "+itemTitle);
        HttpMethods.getInstanceCenter().info.isCollect(spUtils.getString(AppConstant.TOKEN),spUtils.getString(AppConstant.USER_UID),itemCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<IsCollectBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(IsCollectBean isCollectBean) {
                        Log.i("000", "eeeeee: "+isCollectBean.toString());
                          if ("200".equals(isCollectBean.getCode())){
                              isCollect=isCollectBean.isResult();
                          }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("000", "eeeeee: "+e.toString());
                    }

                    @Override
                    public void onComplete() {
                        if (isCollect){
                            //收藏
                            ivShoucang.setImageResource(R.mipmap.icon_shoucang_l);
                        }else{
                            //未收藏
                            ivShoucang.setImageResource(R.mipmap.icon_shoucang);
                        }
                    }
                });
    }

    private void initData() {
        HttpMethods.getInstanceCenter().info.getWorkDetails(/*"001744e8-14b3-4d5e-91ac-e2e38d5c8f41"*/itemId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WorkDetailsBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkDetailsBean workDetailsBean) {
                        if ("200".equals(workDetailsBean.getCode())) {
                            deptBean = workDetailsBean.getResult().getDept();
                            expandBean = workDetailsBean.getResult().getDeptExpand();
                            setTextView(deptBean, expandBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("123", "onError: " + e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    private void setTextView(WorkDetailsBean.ResultBean.DeptBean deptBean, WorkDetailsBean.ResultBean.DeptExpandBean expandBean) {
        tvContent1.setText(deptBean.getQl_name());
        tvContent2.setText(deptBean.getQl_name_yw());
        tvContent3.setText(deptBean.getItem_code());
        tvContent4.setText(WorkDetailsUtils.getTypeAuthority(deptBean.getQl_kind()));
        tvContent5.setText(WorkDetailsUtils.getSourcePower(expandBean.getItemsource()));
        tvContent6.setText(WorkDetailsUtils.getExerciselevel(deptBean.getShiquancj()));
        tvContent7.setText(WorkDetailsUtils.getProcessingType(deptBean.getBjtype()));
        tvContent8.setText(WorkDetailsUtils.getServiceTargetsAreas(deptBean.getQl_object()));
        tvContent9.setText(deptBean.getLawbasis() + deptBean.getQl_lawbasis());
        tvContent10.setText(deptBean.getQxhf());
        tvContent11.setText(deptBean.getSljg());
        tvContent12.setText(WorkDetailsUtils.getInstitutionalNature(deptBean.getSljgxz()));
        tvContent13.setText(deptBean.getJdjg());
        tvContent14.setText(deptBean.getAccept_address());
        tvContent15.setText(deptBean.getAccept_time());
        tvContent16.setText(deptBean.getLink_tel());
    }

    @OnClick({R.id.iv_back, R.id.iv_shoucang, R.id.ll_zixun, R.id.ll_shenqing, R.id.rl_shenqingtiaojian, R.id.rl_shenqingcailiao,R.id.rl_banliliucheng, R.id.rl_shoufeiqingkuang, R.id.rl_zhongjiefuwu, R.id.rl_shenpijieguo, R.id.rl_jiujitujing, R.id.rl_changjianwenti, R.id.rl_banjiegongshi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_shoucang://收藏
                 if (isCollect){
                     Log.i("000", "eeeeee: "+"123123");
                     //取消收藏
                     cancelCollect();

                 }else{
                     //收藏
                     addCollect();

                 }
                break;
            case R.id.ll_zixun://咨询

                break;
            case R.id.ll_shenqing://申请

                break;
            case R.id.rl_shenqingtiaojian:
                b = new Bundle();
                b.putString("text", expandBean.getSqtj());
                b.putString("name", "申请条件");
                ActivityUtils.startActivity(b, TextViewActivity.class);
                break;
            case R.id.rl_shenqingcailiao:
                b = new Bundle();
                b.putString("webUrl", AppConstant.BASE_URL+"app/matter/ywfile?itemGuId="+itemId);
                b.putString("name", "申请材料");
                ActivityUtils.startActivity(b, UserAgreementActivity.class);
                break;
            case R.id.rl_banliliucheng:
                Log.i("123", "onViewClicked: "+deptBean.getFlow_imageurl());
                b = new Bundle();
                b.putString("webUrl", deptBean.getFlow_imageurl());

                b.putString("name", "办理流程");
                ActivityUtils.startActivity(b, UserAgreementActivity.class);
                break;
            case R.id.rl_shoufeiqingkuang:
                b = new Bundle();
                b.putString("webUrl",AppConstant.BASE_URL+"app/matter/ChargeItem?itemGuId="+itemId);
                b.putString("name", "收费情况");
                ActivityUtils.startActivity(b, UserAgreementActivity.class);
                break;
            case R.id.rl_zhongjiefuwu:
                b = new Bundle();
                b.putString("webUrl",AppConstant.BASE_URL+"app/matter/Agency?itemGuId="+itemId);
                b.putString("name", "中介服务");
                ActivityUtils.startActivity(b, UserAgreementActivity.class);
                break;
            case R.id.rl_shenpijieguo:
                b = new Bundle();
                b.putString("webUrl",AppConstant.BASE_URL+"app/matter/approval?itemGuId="+itemId);
                b.putString("name", "审批结果");
                ActivityUtils.startActivity(b, UserAgreementActivity.class);
                break;
            case R.id.rl_jiujitujing:
                b = new Bundle();
                b.putString("text", expandBean.getJjtj());
                b.putString("name", "救济途径");
                ActivityUtils.startActivity(b, TextViewActivity.class);
                break;
            case R.id.rl_changjianwenti:
                b = new Bundle();
                b.putString("webUrl",AppConstant.BASE_URL+"app/matter/Question?itemGuId="+itemId);
                b.putString("name", "常见问题");
                ActivityUtils.startActivity(b, UserAgreementActivity.class);
                break;
            case R.id.rl_banjiegongshi:

                break;
        }
    }

    private void cancelCollect() {
        HttpMethods.getInstanceCenter().info.cancelCollect(spUtils.getString(AppConstant.TOKEN),spUtils.getString(AppConstant.USER_UID),itemCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<IsCollectBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(IsCollectBean isCollectBean) {
                        Log.i("000", "11111: "+isCollectBean.toString());
                        if ("200".equals(isCollectBean.getCode())){
                            isCollect=isCollectBean.isResult();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("000", "11111: "+e.toString());
                    }

                    @Override
                    public void onComplete() {
                    ivShoucang.setImageResource(R.mipmap.icon_shoucang);
                    }
                });
    }

    private void addCollect() {
        HttpMethods.getInstanceCenter().info.addCollect(spUtils.getString(AppConstant.TOKEN),spUtils.getString(AppConstant.USER_UID),itemCode,itemTitle/*,itemId*/)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddCollectBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(AddCollectBean addCollectBean) {
                        Log.i("000", "11111: "+addCollectBean.toString());
                        if ("200".equals(addCollectBean.getCode())){
//                            isCollect=isCollectBean.isResult();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                            ivShoucang.setImageResource(R.mipmap.icon_shoucang_l);
//                            ivShoucang.setImageResource(R.mipmap.icon_shoucang);
                        isCollect=true;
                    }
                });

    }
}
